package com.ophiux.cs.api.consultOrder.impl;

import java.lang.reflect.Type;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.reflect.TypeToken;
import com.ophiux.cs.api.consultOrder.port.IConsultOrderApiPort;
import com.ophiux.cs.application.consultOrder.dto.ApplayCSPublishRequest;
import com.ophiux.cs.application.consultOrder.dto.DoctorOrderCSRequest;
import com.ophiux.cs.application.consultOrder.dto.GetCSOrderRecordsDetailRequest;
import com.ophiux.cs.application.consultOrder.dto.GetCSOrderRecordsDetailResponse;
import com.ophiux.cs.application.consultOrder.dto.GetCSOrderRecordsRequest;
import com.ophiux.cs.application.consultOrder.dto.GetCSOrderRecordsResponse;
import com.ophiux.cs.application.consultOrder.service.IApplayCSPublishAppService;
import com.ophiux.cs.application.consultOrder.service.IDoctorOrderCSAppService;
import com.ophiux.cs.application.consultOrder.service.IGetCSOrderRecordsAppService;
import com.ophiux.cs.application.consultOrder.service.IGetCSOrderRecordsDetailAppService;
import com.ophiux.cs.application.core.Request;
import com.ophiux.cs.application.core.ResponsePage;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.utils.parser.json.JsonUtils;
import com.ophiux.utils.parser.json.JsonUtils.TypeObtainCallback;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class ConsultOrderApiService implements IConsultOrderApiPort{
	@Autowired
	IApplayCSPublishAppService applayCSPublishAppService;
	@Autowired
	IDoctorOrderCSAppService doctorOrderCSAppService;
	@Autowired
	IGetCSOrderRecordsAppService getCSOrderRecordsAppService;
	@Autowired
	IGetCSOrderRecordsDetailAppService getCSOrderRecordsDetailAppService;

	@SuppressWarnings("rawtypes")
	@Override
	public String ApplayCSPublish(String data) {
		log.info("ApplayCSPublish接口,请求参数："+data);
		Result result = null;
		Request<ApplayCSPublishRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<ApplayCSPublishRequest>>() {
					}.getType();
				}
			});
			
			result=applayCSPublishAppService.ApplayCSPublish(request.getPARAM());

			String returnMessage = JsonUtils.toJson(result);
			log.info("ApplayCSPublish接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("ApplayCSPublish接口异常：",e);
			e.printStackTrace();
			result = Result.fail(ResultEnum.requestException);
			return JsonUtils.toJson(result);
		}
	}

	@SuppressWarnings("rawtypes")
	@Override
	public String DoctorOrderCS(String data) {
		log.info("DoctorOrderCS接口,请求参数："+data);
		Result result = null;
		Request<DoctorOrderCSRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<DoctorOrderCSRequest>>() {
					}.getType();
				}
			});
			
			result = doctorOrderCSAppService.DoctorOrderCS(request.getPARAM());

			String returnMessage = JsonUtils.toJson(result);
			log.info("DoctorOrderCS接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("DoctorOrderCS接口异常：",e);
			e.printStackTrace();
			result = Result.fail(ResultEnum.requestException);
			return JsonUtils.toJson(result);
		}
	}

	@Override
	public String GetCSOrderRecords(String data) {
		log.info("GetCSOrderRecords接口,请求参数："+data);
		Result<ResponsePage<GetCSOrderRecordsResponse>> result = null;
		Request<GetCSOrderRecordsRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<GetCSOrderRecordsRequest>>() {
					}.getType();
				}
			});
			
			result = getCSOrderRecordsAppService.GetCSOrderRecords(request.getPARAM());

			String returnMessage = JsonUtils.toJson(result);
			log.info("GetCSOrderRecords接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("GetCSOrderRecords接口异常：",e);
			e.printStackTrace();
			result = Result.fail(ResultEnum.requestException);
			return JsonUtils.toJson(result);
		}
			
	}

	@Override
	public String GetCSOrderRecordsDetail(String data) {
		log.info("GetCSOrderRecordsDetail接口,请求参数："+data);
		Result<GetCSOrderRecordsDetailResponse> result = null;
		Request<GetCSOrderRecordsDetailRequest> request = null;
		try {
			request = JsonUtils.toEntity(data, new TypeObtainCallback() {
				public Type getObtainType() {
					return new TypeToken<Request<GetCSOrderRecordsDetailRequest>>() {
					}.getType();
				}
			});
			
			result = getCSOrderRecordsDetailAppService.GetCSOrderRecordsDetail(request.getPARAM());

			String returnMessage = JsonUtils.toJson(result);
			log.info("GetCSOrderRecordsDetail接口,返回参数："+returnMessage);
			return returnMessage;
		} catch (Exception e) {
			log.info("GetCSOrderRecordsDetail接口异常：",e);
			e.printStackTrace();
			result = Result.fail(ResultEnum.requestException);
			return JsonUtils.toJson(result);
		}
	}

}
